package com.pt25.base.model.security.aspect;

import com.pt25.base.exception.BizException;
import com.pt25.base.model.common.constants.SecurityConstant;
import com.pt25.base.model.security.SecurityService;
import jakarta.annotation.Resource;

import java.util.Set;

class BaseAspect {
    @Resource
    protected SecurityService securityService;

    protected void check(Set<String> roles, String[] permissionArr, boolean and, Set<String> permissionSet) {
        if (!roles.contains(SecurityConstant.ADMIN)) {
            if (null != permissionSet) {
                // 如果为and就需要满足所有的权限，否则只需满足一个权限即可
                if (and) {
                    for (String permission : permissionArr) {
                        boolean isContain = permissionSet.contains(permission);
                        if (!isContain) {
                            throw new BizException("没有权限！！！");
                        }
                    }
                } else {
                    for (String permission : permissionArr) {
                        boolean isContain = permissionSet.contains(permission);
                        if (isContain) {
                            return;
                        }
                    }
                    throw new BizException("没有权限！！！");
                }
            }
        }
    }
}
